package com.anjubao.parking.municipal.parkinglot.controller;

import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.*;
import com.anjubao.parking.municipal.parkinglot.service.ParkingRecordService;
import com.anjubao.parking.municipal.parkinglot.service.ParkingStatRecordService;
import com.anjubao.parking.municipal.parkinglot.service.ParkingStatSlotService;
import com.anjubao.parking.municipal.parkinglot.service.ParkingStatTradesService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 停车场基本信息API
 * @author anjubao
 */
@RestController
@RequestMapping
@Api(value = "ParkingStatController",tags = {"停车场停车记录统计API"})
public class ParkingStatController {

	@Autowired
	private ParkingStatRecordService parkingStatRecordService;
	@Autowired
	private ParkingStatTradesService parkingStatTradesService;
	@Autowired
	private ParkingStatSlotService parkingStatSlotService;
	@Autowired
	private ParkingRecordService parkingRecordService;

	private static final Logger logger = LoggerFactory.getLogger(ParkingStatController.class);

	/**
	 * 根据城市id和停车场类型统计当日各类型停车场停车记录总数
	 * @param cityId
	 * @param type
	 * @param startTime
	 * @param endTime
	 * @return
	 */
    @ApiOperation(value = "统计当日各类型停车场停车记录总数接口",notes = "根据城市ID和停车场类型统计停车记录总数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityId", value = "城市Id", required = true,dataType ="int"),
            @ApiImplicitParam(name = "type", value = "停车场类型 null:聚合,0:拆分,1:封闭式,2:路内",dataType ="int"),
			@ApiImplicitParam(name = "startTime", value = "开始时间 yyyy:MM:dd HH:mm:ss null:当天",dataType ="LocalDateTime"),
			@ApiImplicitParam(name = "endTime", value = "结束时间 yyyy:MM:dd HH:mm:ss",dataType ="LocalDateTime")
    })
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
    @GetMapping("/parking-stat-records")
	public ApiResult<List<ParkingStatRecordsResDto>> getParkingStatRecords(@RequestParam(required = true) Integer cityId, Integer type,
																		   @RequestParam(value = "startTime", required = false) LocalDateTime startTime,
																		   @RequestParam(value = "endTime", required = false) LocalDateTime endTime) {
		logger.info("根据城市ID和停车场类型统计停车记录总数 {} ", cityId,type,startTime,endTime);
		List<ParkingStatRecordsResDto> statRecords = parkingStatRecordService.findStatRecords(cityId, type,startTime,endTime);
			return ApiResult.success(statRecords);
	}

	/**
	 * 根据城市id和停车场类型统计当日各类型停车场费用总数
	 * @param cityId
	 * @param type
	 * @return
	 */
    @ApiOperation(value = "统计当日各类型停车场费用总数接口",notes = "根据城市ID和停车场类型统计停车费用总数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityId", value = "城市Id", required = true,dataType ="int"),
            @ApiImplicitParam(name = "type", value = "停车场类型 null:聚合,0:拆分,1:封闭式,2:路内",dataType ="int")
    })
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
    @GetMapping("/parking-stat-trades")
    public ApiResult<List<ParkingStatTradesResDto>> getParkingStatTrades(@RequestParam(required = true) Integer cityId, Integer type) {
        logger.info("根据城市ID和停车场类型统计停车费用总数 {} ", cityId,type);
		List<ParkingStatTradesResDto> statTrades = parkingStatTradesService.findStatTrades(cityId, type);
		return ApiResult.success(statTrades);
    }

	/**
	 * 根据城市id和市区Id统计当日各区域车流量
	 * @param cityId
	 * @param districtId
	 * @return
	 */
	@ApiOperation(value = "统计当日各区域车流量接口",notes = "根据城市id和市区Id统计当日各区域车流量")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cityId", value = "城市Id", required = true,dataType ="int"),
			@ApiImplicitParam(name = "districtId", value = "市区Id",dataType ="int")
	})
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
	@GetMapping(value = {"/parking-stat-records/zones"})
	public ApiResult<List<ParkingStatRecordsZonesResDto>> getParkingRecordsZones(@RequestParam(required = true) Integer cityId, Integer districtId) {
		logger.info("根据城市id和市区Id统计当日各区域车流量 {} ", cityId,districtId);
		List<ParkingStatRecordsZonesResDto> zonesRecords = parkingStatRecordService.findZonesRecords(cityId, districtId);
		return ApiResult.success(zonesRecords);
	}

	/**
	 * 根据城市id和停车场类型统计当日各类型停车场空位
	 * @param cityId
	 * @param type
	 * @return
	 */
	@ApiOperation(value = "统计当日各类型停车场空位接口",notes = "根据城市ID和停车场类型统计停车场空位")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cityId", value = "城市Id", required = true,dataType ="int"),
			@ApiImplicitParam(name = "type", value = "停车场类型 null:聚合,0:拆分,1:封闭式,2:路内",dataType ="int")
	})
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
	@GetMapping("/parking-stat-slots/free")
	public ApiResult<List<ParkingStatSlotsFreeResDto>> getParkingSlotsFree(@RequestParam(required = true) Integer cityId, Integer type) {
		logger.info("根据城市ID和停车场类型统计停车场空位 {} ", cityId,type);
		List<ParkingStatSlotsFreeResDto> slotsFree = parkingStatSlotService.findSlotsFree(cityId, type);
		return ApiResult.success(slotsFree);
	}

	/**
	 * 根据城市id统计当日各地区停车场空位
	 * @param cityId
	 * @return
	 */
	@ApiOperation(value = "统计当日各地区停车场空位接口",notes = "根据城市ID统计各地区停车场空位")
	@ApiImplicitParam(name = "cityId", value = "城市Id", required = true,dataType ="int")
	@GetMapping("/parking-stat-slots/zones")
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
	public ApiResult<List<ParkingStatSlotsZonesResDto>> getParkingSlotsZones(@RequestParam(required = true) Integer cityId) {
		logger.info("根据城市ID统计各地区停车场空位 {} ", cityId);
		List<ParkingStatSlotsZonesResDto> slotsZones = parkingStatSlotService.findSlotsZones(cityId);
		return ApiResult.success(slotsZones);
	}

	/**
	 * 根据城市id和tab统计重点停车场车位空位列表
	 * @param cityId
	 * @param tab
	 * @return
	 */
	@ApiOperation(value = "统计重点停车场车位空位列表接口",notes = "根据城市id和tab统计重点停车场车位空位列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cityId", value = "城市Id", required = true,dataType ="int"),
			@ApiImplicitParam(name = "tab", value = "业务块 1:重点项目车位压力,2：重点交通枢纽车位压力",required = true,dataType ="int")
	})
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
	@GetMapping("/parking-stat-slots/focus")
	public ApiResult<List<ParkingStatSlotsFocusResDto>> getParkingSlotsFocus(@RequestParam(required = true) Integer cityId, @RequestParam(required = true) Integer tab) {
		logger.info("根据城市id和tab统计重点停车场车位空位列表 {} ", cityId,tab);
		List<ParkingStatSlotsFocusResDto> slotsFocus = parkingStatSlotService.findSlotsFocus(cityId,tab);
		return ApiResult.success(slotsFocus);
	}

	/**
	 * 统计包含停车场数和车位数 按地区统计
	 @param cityId
	 * @return
	 */
	@ApiOperation(value = "按地区统计停车场数和车位数 ",notes = "统计停车场数和车位数")
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
	@GetMapping("/parking-stat-parking-lots/zones")
	public ApiResult<List<ParkingStatDistrictParkingLotResDto>> getParkingStatParkingLots(@RequestParam(required = true) @ApiParam(value = "城市id") Integer cityId) {
		List<ParkingStatDistrictParkingLotResDto> slotsFocus = parkingStatSlotService.getParkingStatParkingLots(cityId);
		return ApiResult.success(slotsFocus);
	}

	/**
	 *【运营数据】-9 停车时长占比（流量） 参数【开始日期、结束日期】 默认 今天-今天
	 * parking_record  按leave_parking_duration统计[<30,30-60,60-90,>90]，where日期范围统计
	 * /gateway/parking-analysis-api/parking-stat-records/cost-time
	 * -----yapi: http://172.25.2.136:8000/project/12/interface/api/1740
	 */
	@ApiOperation("停车时长占比（流量）")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cityId", value = "城市Id", required = true,dataType ="int"),
			@ApiImplicitParam(name = "startTime", value = "开始时间",dataType ="LocalDateTime"),
			@ApiImplicitParam(name = "endTime", value = "结束时间",dataType ="LocalDateTime")
	})
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
	@GetMapping("/parking-stat-records/cost-time")
	public ApiResult<List<ParkingRecordCostTimeStatDto>> getParkingCostTimeStat(@RequestParam(name = "cityId", required = true) Integer cityId,
																				@RequestParam(name = "startTime", required = false) LocalDateTime startTime,
																				@RequestParam(name = "endTime", required = false) LocalDateTime endTime) {
		logger.info("统计停车时长占比 {} ", cityId,startTime,endTime);
		if (null == startTime) {
			startTime = LocalDateTime.now().minusDays(100L);
		}
		if (null == endTime) {
			endTime = LocalDateTime.now();
		}
		return ApiResult.success(parkingRecordService.getParkingCostTimeStat(cityId, startTime, endTime));
	}

	/**
	 * 【运营数据】-5 泊位周转率
	 * @param startTime
	 * @param endTime
	 * @param cityId
	 * @return
	 */
	@ApiOperation(value = "泊位周转率统计",notes = "统计单位时间泊位周转率")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cityId", value = "城市Id", required = true,dataType ="int"),
			@ApiImplicitParam(name = "startTime", value = "开始时间",dataType ="LocalDateTime"),
			@ApiImplicitParam(name = "endTime", value = "结束时间",dataType ="LocalDateTime")
	})
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
	@GetMapping("/parking-stat-slots/turnover")
	public ApiResult<ParkingLotSlotTurnoverDto> getParkingSlotTurnover(Integer cityId, @RequestParam(value = "startTime", required = false) LocalDateTime startTime,
																	   @RequestParam(value = "endTime", required = false) LocalDateTime endTime){
		logger.info("统计单位时间泊位周转率 {} ", cityId,startTime,endTime);
		return ApiResult.success(parkingStatSlotService.getParkingSlotTurnover(cityId,startTime,endTime));
	}

	/**
	 * 【运营数据】-10 服务数据
	 * @param startTime
	 * @param endTime
	 * @param cityId
	 * @return
	 */
	@ApiOperation(value = "服务数据统计",notes = "实时统计服务数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cityId", value = "城市Id", required = true,dataType ="int"),
			@ApiImplicitParam(name = "startTime", value = "开始时间",dataType ="LocalDateTime"),
			@ApiImplicitParam(name = "endTime", value = "结束时间",dataType ="LocalDateTime")
	})
	@ApiResponses(value = {
			@ApiResponse(code = 5000,message = "操作失败")
	})
	@GetMapping("/parking-stat/service")
	public ApiResult<ParkingStatServiceDto> getParkingStat(Integer cityId, @RequestParam(value = "startTime", required = false) LocalDateTime startTime,
														   @RequestParam(value = "endTime", required = false) LocalDateTime endTime) {
		logger.info("实时统计服务数据 {} ",cityId,startTime,endTime);
		return ApiResult.success(parkingRecordService.getParkingStat(cityId,startTime,endTime));
	}

}